using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace BlockOS.Client
{
    [RequireComponent(typeof(MeshFilter), typeof(MeshCollider), typeof(MeshRenderer))]
    public class ChunkObj : MonoBehaviour
    {
        private MeshRenderer m_meshRenderer;
        private MeshCollider m_meshCollider;
        private MeshFilter m_meshFilter;

        private MeshBuilder m_builder;


        public MeshRenderer meshRenderer
        {
            get
            {
                if (m_meshRenderer == null) m_meshRenderer = GetComponent<MeshRenderer>();
                return m_meshRenderer;
            }
        }
        public MeshCollider meshCollider
        {
            get
            {
                if (m_meshCollider == null) m_meshCollider = GetComponent<MeshCollider>();
                return m_meshCollider;
            }
        }
        public MeshFilter meshFilter
        {
            get
            {
                if (m_meshFilter == null) m_meshFilter = GetComponent<MeshFilter>();
                return m_meshFilter;
            }
        }

        private void Awake()
        {
            m_builder = new MeshBuilder();
        }

        private void Start()
        {
            ResetTexture();
        }

        public void ResetTexture()
        {
            StartCoroutine(CoDelayedSetTexture());
        }

        private IEnumerator CoDelayedSetTexture()
        {
            var res = GameClient.Instance.Res;
            yield return new WaitUntil(() => res.IsDone);
            meshRenderer.material.mainTexture = res.chunkTexture;
        }

        public void RefreshMesh(ChunkData chunkData)
        {

        }
    }
}
